package com.ylz.tedi.myapplication;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class BoFangAdpater extends RecyclerView.Adapter<BoFangAdpater.BoFangViewHolder>{

    private Context context;
    private ArrayList<String> arrayList;

    public ArrayList<PlayerVideoThread> getList() {
        return list;
    }

    private ArrayList<PlayerVideoThread> list;

    public BoFangAdpater(Context context, ArrayList<String> arrayList){
        this.arrayList=arrayList;
        this.context=context;
        list=new ArrayList<>();
    }

    @NonNull
    @Override
    public BoFangViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.adapter,viewGroup,false);
        BoFangViewHolder holder=new BoFangViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BoFangViewHolder boFangViewHolder, int i) {
//        CeShi ceShi= new CeShi(context,arrayList.get(i));
//        list.add(ceShi.getThread());
//        boFangViewHolder.textureView.setSurfaceTextureListener(ceShi);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class BoFangViewHolder extends RecyclerView.ViewHolder{
        private TextureView textureView;
        public BoFangViewHolder(@NonNull View itemView) {
            super(itemView);
            textureView=itemView.findViewById(R.id.textureview);
        }
    }
    /**
     * 定义TextureView监听类SurfaceTextureListener
     * 重写4个方法
     */
    private TextureView.SurfaceTextureListener surfaceTextureListener=new TextureView.SurfaceTextureListener(){

        private PlayerVideoThread thread;

        /**
         * 初始化好SurfaceTexture后调用
         * @param surfaceTexture
         * @param i
         * @param i1
         */
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
            //开启一个线程去播放视频
            thread=new PlayerVideoThread(context,"rtsp://192.168.1.100/mpeg4cif",surfaceTexture);
            thread.start();
        }

        /**
         * 视频尺寸改变后调用
         * @param surfaceTexture
         * @param i
         * @param i1
         */
        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

        }

        /**
         * SurfaceTexture即将被销毁时调用
         * @param surfaceTexture
         * @return
         */
        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {

//            mMediaPlayer.stop();
//            mMediaPlayer.release();
//            mMediaPlayer=null;
            thread.stops();
            return true;
        }

        /**
         * 通过SurfaceTexture.updateteximage()更新指定的SurfaceTexture时调用
         * @param surfaceTexture
         */
        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
            Log.d("dadada","dadadada");
        }
    };
}
